//
//  CXHGroMoreBannerAdapter.m
//  CXHAdSDK-GroMore
//
//  Created by 陈坤 on 2024/10/15.
//

#import "CXHGroMoreBannerAdapter.h"
#import <CXHAdSDK-Core/CXHAdSDKBannerAdView.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>

@interface CXHGroMoreBannerAdapter ()<CXHSDKBannerAdViewDelegate>

@property (nonatomic, strong) CXHAdSDKBannerAdView *bannerAdView;

@end

@implementation CXHGroMoreBannerAdapter

- (BUMMediatedAdStatus)mediatedAdStatus {
    return BUMMediatedAdStatusNormal;
}

- (void)loadBannerAdWithSlotID:(nonnull NSString *)slotID andSize:(CGSize)adSize parameter:(nullable NSDictionary *)parameter {
    
    if (self.bannerAdView) {
        [self.bannerAdView removeFromSuperview];
        self.bannerAdView.delegate = nil;
        self.bannerAdView = nil;
    }
    
    if (CGSizeEqualToSize(adSize, CGSizeZero)) adSize = CGSizeMake(kCXHScreenWidth, kCXHScreenWidth * 5 / 32);
    
    NSString *jsonStr = [parameter CXH_objectOrNilForKey:@"custom_json"];
    //json解析
    NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
           
    NSError *err;
           
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&err];
    
    BOOL showCloseView = YES;
    if (err == nil && !kCXHDictIsEmpty(jsonDict)) {
        NSString *str = [jsonDict CXH_objectOrNilForKey:@"showCloseView"];
        if ([str isEqualToString:@"false"]) {
            showCloseView = NO;
        }
    }

    self.bannerAdView = [[CXHAdSDKBannerAdView alloc]initWithFrame:CGRectMake(0, 0, adSize.width, adSize.height)];
    self.bannerAdView.controller = self.bridge.viewControllerForPresentingModalView;
    self.bannerAdView.delegate = self;
    self.bannerAdView.showCloseView = showCloseView;
    self.bannerAdView.posId = slotID;
    [self.bannerAdView loadAndShow];
}

- (void)didReceiveBidResult:(BUMMediaBidResult *)result {
    // 在此处理Client Bidding的结果回调
    if (result.win) {
        NSInteger winPrice = self.bannerAdView.getBidPrice;
        if (result.winnerPrice) {
            winPrice = result.winnerPrice;
        }
        [self.bannerAdView sendWinNotice:winPrice];
    } else {
        if (result.winnerPrice) {
            [self.bannerAdView sendLossNotice:result.winnerPrice];
        }
    }
}

#pragma mark - CXHSDKBannerAdViewDelegate
/**
 广告获取成功
 
 @param bannerView banner实例
 */
- (void)CXH_bannerViewDidReceived:(CXHAdSDKBannerAdView *)bannerView{
    [self.bridge bannerAd:self didLoad:self.bannerAdView ext:@{
        BUMMediaAdLoadingExtECPM : [NSString stringWithFormat:@"%ld", self.bannerAdView.getBidPrice],
    }];
}

/**
 广告拉取失败
 
 @param bannerView banner实例
 @param error 错误描述
 */
- (void)CXH_bannerViewFailToReceived:(CXHAdSDKBannerAdView *)bannerView error:(NSError *)error{
    [self.bridge bannerAd:self didLoadFailWithError:error ext:@{}];
}

/**
 广告点击
 
 @param bannerView 广告实例
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)CXH_bannerViewClicked:(CXHAdSDKBannerAdView *)bannerView loadingPageURL:(NSString *)loadingPageURL{
    [self.bridge bannerAdDidClick:self bannerView:bannerView];
    [self.bridge bannerAdWillPresentFullScreenModal:self bannerView:bannerView];
}

/**
 广告点击上报
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewClickedReport:(CXHAdSDKBannerAdView *)bannerView{
    
}

/**
 广告关闭
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewClose:(CXHAdSDKBannerAdView *)bannerView{
    [self.bridge bannerAd:self bannerView:bannerView didClosedWithDislikeWithReason:@[]];
}

/**
 广告展示
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewExposure:(CXHAdSDKBannerAdView *)bannerView{
    [self.bridge bannerAdDidBecomeVisible:self bannerView:bannerView];
    
}

/**
 广告展示上报
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerViewExposureReport:(CXHAdSDKBannerAdView *)bannerView{
    
}

/**
 关闭落地页
 
 @param bannerView 广告实例
 */
- (void)CXH_bannerAdCloseLandingPage:(CXHAdSDKBannerAdView *)bannerView{
    
}

@end
